package com.gongj.xml.xmlmain;

import com.gongj.xml.bean.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.XmlReaderContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.MethodInvoker;

import java.lang.annotation.Annotation;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Supplier;

public class Maiin {

	public static void main(String[] args) {

		ClassPathXmlApplicationContext context =
				new ClassPathXmlApplicationContext("spring-config.xml");
		Person person = (Person)context.getBean("person");
		System.out.println(person + "===="+ person.hashCode());
		Person person2 = (Person)context.getBean("person");
		System.out.println(person2 + "===="+ person2.hashCode());

		Person person3 = (Person)context.getBean("person",8599);
		System.out.println(person3 + "===="+ person3.hashCode());
	}


	public static void main1(String[] args) {
/*		Object[] objects = new Object[]{new A()};

// 0
		System.out.println(MethodInvoker.getTypeDifferenceWeight(new Class[]{A.class}, objects));

// 2
		System.out.println(MethodInvoker.getTypeDifferenceWeight(new Class[]{B.class}, objects));

// 4
		System.out.println(MethodInvoker.getTypeDifferenceWeight(new Class[]{C.class}, objects));

// 1
		System.out.println(MethodInvoker.getTypeDifferenceWeight(new Class[]{D.class}, objects));
		*/
		ClassPathXmlApplicationContext context =
				new ClassPathXmlApplicationContext("spring-config.xml");
		Object person = context.getBean("person");
		Object person3 = context.getBean("person");
		Object person2 = context.getBean("person",5555);

		System.out.println("最终对象："+person);
		System.out.println("最终对象："+person2);


		/*测试使用Supplier*/
		// 创建 BeanFactory 容器
		DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
		// 构建BeanDefinition
		RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
		rootBeanDefinition.setBeanClass(User.class);
		rootBeanDefinition.setInstanceSupplier(new Supplier<Person>() {
			@Override
			public Person get() {
				return new Person(555);
			}
		});
		// 注册BeanDefinition
		factory.registerBeanDefinition("user",rootBeanDefinition);
		Object user = factory.getBean("user");
		// 返回的是 Person 对象
		System.out.println("结果：" + user);


	}

	public static void main2(String[] args) {
		  Set<Class<? extends Annotation>> autowiredAnnotationTypes = new LinkedHashSet<>(4);

		autowiredAnnotationTypes.add(Autowired.class);
		autowiredAnnotationTypes.add(Value.class);
		boolean candidateClass = AnnotationUtils.isCandidateClass(Ordered.class, autowiredAnnotationTypes);

		System.out.println(candidateClass);
		Class<?> orderedClass = Ordered.class;
		System.out.println(org.springframework.core.Ordered.class == org.springframework.core.Ordered.class);
	}
}
